<?php

namespace view;

/**
 * <p>bazowa klasa widoku</p>
 *
 * @author Damian Jóźwiak
 */
class View extends \library\Base{
    
    /**
     * @readwrite
     */
    protected $file;
    
    /**
     * @read
     */
    protected $template;
    protected $data=array();
    public function _getExceptionForArgument(){
        return new exceptions\WrongArgumentException();
    }
    public function __construct($options = array()) {
        parent::__construct($options);
        $this->template=new template\Template(array(
            "implementation" =>new template\Extended()
        ));
    }
    public function render(){
        if(!file_exists($this->getFile())){
            return "";
        }
        $content = file_get_contents($this->getFile());
       // var_dump($content);
        $this->template->parse($content);
        return $this->template->process($this->data);
    }
    public function get($key){
        return \library\ArrayMethods::getElement($key, $this->data);
    }
    protected function _set($key,$value){
        if (!is_string($key) && !is_numeric($key)){
            throw new exceptions\DataException("klucz musi być stringiem lub liczbą");
        }
        $this->data[$key]=$value;
    }
    public function set($key,$value=null){
        if (is_array($key)){
            foreach ($key as $_key => $_value){
                $this->_set($_key, $_value);
            }
            return $this;
        }
        $this->_set($key, $value);
        return $this;
    }
    public function erase($key){
       unset($this->data[$key]);
       return $this;
    }
    public static function showErrors($array,$key,$separator ="<br/>",$before="",$after=""){
        if (isset($array) && isset($array[$key])){
            return $before.implode($separator, $array[$key]).$after;
        }
    }
}
